package com.tanovo.config;

import com.alibaba.fastjson.JSONObject;
import com.tanovo.bo.UserCore;
import feign.RequestInterceptor;
import feign.RequestTemplate;

public class UserInfoFeighClientIntercepter implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //从应用上下文中取出user信息，放入Feign的请求头中
        UserCore user = UserInfoContext.getUser();
        if (user != null) {
            String userJson = JSONObject.toJSONString(user);
            requestTemplate.header(UserInfoContext.KEY_USERINFO_IN_HTTP_HEADER, userJson);
        } else {
            UserCore emptyUser = new UserCore();
            emptyUser.setId(-1);
            emptyUser.setAge(-1);
            emptyUser.setCompanyId(-1);
            emptyUser.setClientId("");
            emptyUser.setPhone("");
            emptyUser.setLoginName("");
            emptyUser.setEmail("");
            emptyUser.setRequestURI("");
            emptyUser.setMark("");
            String userJson = JSONObject.toJSONString(emptyUser);
            requestTemplate.header(UserInfoContext.KEY_USERINFO_IN_HTTP_HEADER, userJson);
        }
    }
}
